package com.itdct.onflow.core.context

import com.itdct.onflow.core.dto.BaseLoginDTO

/**
 * @author Zhouwx
 * @date 2025/4/29 15:43:48
 * @version 1.0
 * @description
 */
class ContextHolder {
    companion object {
        // 使用独立的方法级别泛型
        private val threadLocal = ThreadLocal<BaseContext<*, *>>()

        // 每个方法声明自己的泛型参数
        fun <AccountEntity : Any, LoginDTO : BaseLoginDTO<AccountEntity>> getContext(): BaseContext<AccountEntity, LoginDTO>? {
            @Suppress("UNCHECKED_CAST")
            return threadLocal.get() as? BaseContext<AccountEntity, LoginDTO>
        }

        fun <AccountEntity : Any, LoginDTO : BaseLoginDTO<AccountEntity>> setContext(context: BaseContext<AccountEntity, LoginDTO>) {
            threadLocal.set(context)
        }

        fun removeContext() {
            threadLocal.remove()
        }
    }
}